package com.foreknow.dao.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
    @Override
    public boolean add(Guestbook guestbook) throws SQLException {
        int isRigth = jdbcTemplate.update("insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?)",
                guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),
                guestbook.getContent(),guestbook.getCreatedtime());
        return isRigth == 1;
    }

    @Override
    public List<Object> getAll() {
        EntityMapping entityMapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);// GuestbookMapping对象
        List<Object> list = jdbcTemplate.query("select * from guestbook",entityMapping);
        return list;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        int isRight = jdbcTemplate.update("delete from guestbook where id=?",id);
        return isRight == 1;
    }

    @Override
    public Guestbook getQueryById(int id) {
        EntityMapping entityMapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from guestbook where id=?",entityMapping,id);
        if(list.size()>0) {
            return (Guestbook) list.get(0);
        }
        return null;
    }

    @Override
    public boolean update(Guestbook gb) throws SQLException {
        int isRight = jdbcTemplate.update("update guestbook set name=?,phone=?,email=?,title=?,content=?,createdtime=? where id=?",
                gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedtime(),gb.getId());
        return isRight == 1;
    }


}
